package com.zyf.springcloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyf.springcloud.model.LoginData;
import com.zyf.springcloud.model.User;
import com.zyf.springcloud.model.selectObject;
import com.zyf.springcloud.result.Result;
import com.zyf.springcloud.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/educationUser")
public class UserController {
    @Autowired
    UserServiceImpl userService;
    @PostMapping("/login")
    public Result login(@RequestBody LoginData data){
        return userService.login(data);
    }

    @GetMapping("/getAll")
    public Result getAll() {
        return userService.getAll();
    }

    @PostMapping("/getPage/{current}/{limit}")
    public Result getPage(@PathVariable int current, @PathVariable int limit,
                          @RequestBody(required = false) selectObject selectObject) {
        return userService.getPage(current, limit, selectObject);
    }
    @PutMapping("/update/{id}")
    public Result updateUserById(@PathVariable Long id,@RequestBody LoginData data) {
        return userService.updateUserById(id,data);
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteUserByOne(@PathVariable("id") Long id) {
        return userService.deleteUserByOne(id);
    }

    @PutMapping("/add")
    public Result addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @GetMapping("/selectByName/{username}")
    public User selectByName(@PathVariable("username") String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        Result result = userService.selectByOne(queryWrapper);
        return (User) result.getData();
    }
}
